package com.ruoyi.goods.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.goods.mapper.CarInfoMapper;

import com.ruoyi.goods.pojo.CarInfoVo;
import com.ruoyi.goods.pojo.Results;
import com.ruoyi.goods.service.CarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CarInfoServiceImpl implements CarInfoService {
    @Autowired
    private CarInfoMapper carInfoMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Results findCarPage() {
        String string = redisTemplate.opsForValue().get("carinfo:list");
        if (StrUtil.isEmpty(string)){
            List<CarInfoVo> list = carInfoMapper.seleAll();
            redisTemplate.opsForValue().set("carinfo:list", JSON.toJSONString(list),60, TimeUnit.HOURS);
            redisTemplate.opsForValue().set("carinfo:count",1+"");
            return new Results(200,"查询成功",list);
        }else {
            List<CarInfoVo> list = JSON.parseArray(string, CarInfoVo.class);
            redisTemplate.opsForValue().increment("carinfo:count");
            return new Results(200,"查询成功",list);
        }

    }
}
